package com.shanzhu.volunteer.modules.ai.controller;

import com.shanzhu.volunteer.common.annotation.Log;
import com.shanzhu.volunteer.common.annotation.RepeatSubmit;
import com.shanzhu.volunteer.common.core.domain.R;
import com.shanzhu.volunteer.common.enums.BusinessType;
import com.shanzhu.volunteer.modules.ai.dto.AIEvaluationRequestDTO;
import com.shanzhu.volunteer.modules.ai.dto.EvaluationResultDTO;
import com.shanzhu.volunteer.modules.ai.dto.StudentProfileDTO;
import com.shanzhu.volunteer.modules.ai.service.AIModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 测评分析控制器
 */
@Api(tags = "测评分析管理")
@RestController
@RequestMapping("/api/evaluation")
public class EvaluationController {
    
    @Autowired
    private AIModelService aiModelService;
    
    /**
     * AI智能测评
     */
    @ApiOperation("AI智能测评")
    @PostMapping("/ai")
    @RepeatSubmit(interval = 1000)
    @Log(title = "AI智能测评", businessType = BusinessType.OTHER)
    public R<EvaluationResultDTO> aiEvaluate(@RequestBody AIEvaluationRequestDTO request) {
        // 参数校验
        if (request.getStudentNo() == null || request.getStudentNo().isEmpty()) {
            return R.fail("学生学号不能为空");
        }
        
        // 设置默认值
        if (request.getDimensions() == null || request.getDimensions().isEmpty()) {
            request.setDimensions(Arrays.asList("interest", "career"));
        }
        
        if (request.getCount() <= 0) {
            request.setCount(5);
        }
        
        if (request.getDepth() <= 0) {
            request.setDepth(3);
        }
        
        // 调用服务执行AI分析
        EvaluationResultDTO result = aiModelService.analyzeStudentProfile(request);
        return R.ok(result);
    }
    
    /**
     * 获取学生基本信息
     */
    @ApiOperation("获取学生基本信息")
    @GetMapping("/student/{studentNo}")
    public R<StudentProfileDTO> getStudentProfile(@PathVariable String studentNo) {
        if (studentNo == null || studentNo.isEmpty()) {
            return R.fail("学生学号不能为空");
        }
        
        // 模拟学生信息
        StudentProfileDTO profile = new StudentProfileDTO();
        profile.setId(1L);
        profile.setStudentNo(studentNo);
        profile.setStudentName("测试学生");
        profile.setGrade("高三");
        
        // 基于学号生成稳定的随机分数
        int hashCode = Math.abs(studentNo.hashCode());
        int score = 500 + hashCode % 200; // 分数在500-700之间
        int rank = 3000 - score;          // 排名与分数成反比
        
        profile.setScore(score);
        profile.setRank(rank);
        profile.setTagNameText("理科,计算机爱好者,编程");
        
        return R.ok(profile);
    }
    
    /**
     * 获取分析维度列表
     */
    @ApiOperation("获取分析维度列表")
    @GetMapping("/dimensions")
    public R<List<Map<String, Object>>> getAnalysisDimensions() {
        List<Map<String, Object>> dimensions = new ArrayList<>();
        
        // 定义分析维度选项
        addDimension(dimensions, "interest", "学科兴趣匹配度", "分析学生兴趣与专业的匹配程度");
        addDimension(dimensions, "career", "就业前景分析", "评估专业的就业前景和发展潜力");
        addDimension(dimensions, "environment", "院校环境适应性", "分析学生与院校环境的适应能力");
        addDimension(dimensions, "potential", "专业发展潜力", "评估学生在该专业的发展潜力");
        addDimension(dimensions, "personality", "个人性格匹配度", "分析学生性格与专业特点的匹配程度");
        
        return R.ok(dimensions);
    }
    
    /**
     * 添加维度信息
     */
    private void addDimension(List<Map<String, Object>> dimensions, String id, String name, String description) {
        Map<String, Object> dimension = new HashMap<>();
        dimension.put("id", id);
        dimension.put("name", name);
        dimension.put("description", description);
        dimensions.add(dimension);
    }
} 